package com.zelin.manager.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.zelin.manager.service.ItemService;
import com.zyg.common.utils.PageUtils;
import com.zyg.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zelin.manager.entity.GoodsEntity;
import com.zelin.manager.service.GoodsService;



/**
 * 商品
 *
 * @author hbxfwf
 * @email hbxfwf@sina.com
 * @date 2021-10-22 16:59:59
 */
@RestController
@RequestMapping("manager/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ItemService itemService;
    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("manager:goods:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = goodsService.queryPage(params);
        // List<GoodsEntity> list = (List<GoodsEntity>) page.getList();
        // List<GoodsEntity> collect = list.stream().filter(f -> !f.getAuditStatus().equals("1")).collect(Collectors.toList());
        // page.setList(collect);

        return R.ok().put("page", page);
    }

    //商品审核
    @GetMapping("/updateStatus")
    public R updateStatus(Long id,String status){
        //1. 修改spu商品状态
        goodsService.updateStatus(id,status);
        //2. 根据spu商品id查询sku列表，并将其添加到索引库中
        itemService.updateToIndex(id);
        return R.ok();
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("manager:goods:info")
    public R info(@PathVariable("id") Long id){
		GoodsEntity goods = goodsService.getById(id);

        return R.ok().put("goods", goods);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("manager:goods:save")
    public R save(@RequestBody GoodsEntity goods){
		goodsService.save(goods);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    //@RequiresPermissions("manager:goods:update")
    public R update(@RequestBody GoodsEntity goods){
		goodsService.updateById(goods);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    //@RequiresPermissions("manager:goods:delete")
    public R delete(@RequestBody Long[] ids){
        //1. 处理逻辑删除（对tb_goods表的is_delete修改为1）
		goodsService.removeByIds(Arrays.asList(ids));
        //2. 更新到索引库
        itemService.removeFromIndex(ids);
        return R.ok();
    }

}
